package com.x.client.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.client.model.SelectTemplate;
import com.x.client.rpc.SelectTemplateRpcService;
import com.x.erp.model.Car;
import com.x.erp.model.ClientSelectTemplate;
import com.x.erp.model.Price;
import com.x.erp.rpc.CarRpcService;
import com.x.erp.rpc.ClientCarRpcService;
import com.x.erp.rpc.PriceRpcService;
import com.x.erp.util.ConstantState;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/clientCar")
public class ClientCarController extends BaseController {

	@Reference
	private SelectTemplateRpcService selectTemplateRpcService; // 订阅RpcService

	@Reference
	private ClientCarRpcService clientCarRpcService;// C端车辆信息服务

	@Reference
	private CarRpcService carRpcService;// 车辆信息服务

	@Reference
	private PriceRpcService priceRpcService;// 价格服务

	/**
	 * 根据用户Id信息查询订阅车辆列表
	 *
	 * @param car
	 * @return
	 */
	@RequestMapping(value = {"/getCarListBySelectTemplate"}, method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel selectCarListBySelectTemplate(Car car) {
		if(StringUtils.isEmpty (car.getUserId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "用户Id不能为空！");
		}
		SelectTemplate selectTemplate = new SelectTemplate();
		selectTemplate.setUserId(car.getUserId());
		car.setUserId(null);
		// 先查询出订阅信息
		selectTemplate = selectTemplateRpcService.selectSelectTemplate(selectTemplate);
		if(selectTemplate == null){
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功！", new ArrayList<>());
		}
		// 讲订阅信息复制到erp的客户端订阅信息类里
		ClientSelectTemplate temp = new ClientSelectTemplate();
		BeanUtils.copyProperties(selectTemplate, temp);
		// 根据C端订阅信息查询车辆列表
		List<Car> res = clientCarRpcService.selectCarListBySelectTemplate(car, temp);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功！", res);
	}

	/**
	 * 查询车辆列表信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = {"/getCarList"}, method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getCarList(Car car) {
		List<Car> cars = clientCarRpcService.selectCarList(car);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", cars);
	}

	/**
	 *
	 * 猜你喜欢：展示该市场的其它价格属于一个分类的6辆车
	 * @param
	 * @return
	 */
	@RequestMapping(value = {"/gussYouLike"}, method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel gussYouLike(Car car) {
		BigDecimal retailPrice = car.getCarRetailPrice ();
		if(retailPrice == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆零售价不能为空！");
		}
		if(StringUtils.isEmpty (car.getMarketId ())){
			return new ResultModel(ResultCode.RESULT_FAIL, "市场Id不能为空！");
		}
		// 根据零售价价格，查找价格区间
		Price priceTemp = new Price ();
		priceTemp.setDetailPrice (retailPrice);
		priceTemp = priceRpcService.selectPriceByDetailPrice (priceTemp);

		// 设置查询条件，价格区间
		car.setFromRetailPrice (priceTemp.getPriceFrom10000 ());
		car.setToRetailPrice (priceTemp.getPriceTo10000 ());
		// 将查询条件零售价置为null
		car.setCarRetailPrice (null);
		// C端只查询以上架的车辆
		car.setCarSaleState (ConstantState.CAR_SALE_STATE_ONSALE_FINISH);
		List<Car> cars = carRpcService.selectCarList(car);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", cars);
	}
}
